# -*- coding: UTF-8 -*-

import os
import argparse

if __name__ == '__main__':  
    
    argument_parser = argparse.ArgumentParser(description='text files combine tool')
    argument_parser.add_argument('-s', dest = 'srcDir', help='the folder name of the files to be combined')
    argument_parser.add_argument('-d', dest = 'destFile', help='the destination file name')
    
    args = argument_parser.parse_args()  
    
    srcDirName = args.srcDir
    destFileName = args.destFile
    
    allfiles = os.listdir(srcDirName)
    allfiles.sort()
    
    '''
    for eachfile in allfiles:
        print os.path.join(srcDirName, eachfile)
    '''
    
    destFile = open(destFileName, 'w')
    for eachfile in allfiles:
        print eachfile
        sourceFilePathName = os.path.join(srcDirName, str(eachfile).decode('GBK'))
        
        if (os.path.getsize(sourceFilePathName) > 2048):
            #print sourceFilePathName
            srcFile = open(sourceFilePathName, 'r')
            
            # write file name at the beginning
            #destFile.write(u'\n')
            #section = str(eachfile.strip('.txt')).decode('GBK', 'ignore').encode('UTF-8')            
            #destFile.write(section)
            #destFile.write(u'\n')
            
            # write the file content
            destFile.write(srcFile.read())
            srcFile.close()            
        else:
            print 'Ignore ' + sourceFilePathName
        
        # delete the original file
        os.remove(sourceFilePathName)
        
        # flush the data to file
        destFile.flush()
        
        # check the file size, stop if size over 8MB
        if (os.path.getsize(destFileName) >= 8*1024*1024):
            break
        
    destFile.close()
    